﻿//------------------------------------------------------------------------------
// The contents of this file are subject to the Microsoft Public License (Ms-PL)
// You may obtain a copy of the License at http://psadmin.codeplex.com/license. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, 
// WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations 
// under the License.
// 
// The initial developer is Ben Foster (ben.foster@retroviz.com)
// Copyright (c) Retroviz Limited (http://www.retroviz.com).
// All Rights Reserved.
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PSAdmin.Core.Interfaces;
using System.Web.Security;
using System.Configuration;

namespace PSAdmin.Web.Infrastructure
{
    public class SiteStartUpTask : IStartUpTask
    {
        public bool IsEnabled
        {
            get { return true; }
        }

        public void Execute()
        {
            var adminRole = "Administrators";

            if (!Roles.RoleExists(adminRole)) {
                Roles.CreateRole(adminRole);
            }

            var adminUsers = ConfigurationManager.AppSettings["AdminUsers"];

            if (!string.IsNullOrEmpty(adminUsers))
            {
                foreach (var user in adminUsers.ToLower().Split(','))
                {
                    if (!Roles.IsUserInRole(user, adminRole))
                        Roles.AddUserToRole(user, adminRole);
                }
            }
        }
    }
}
